<?php
namespace Admin\Controller;
use Admin\Controller\BaseController;

class AcademyController extends BaseController {

	public function index() {
		$academies = M('Academy')->select();
		$this->assign('academies', $academies);
		$this->display();
	}

	public function addPost() {
		if(!IS_POST) {
			$this->redirect('Academy/index');
		}
		$academy = D('Academy');
		if(!$academy->create()) {
			$this->error($academy->getError());
		}
		if(!$academy->add()) {
			$this->success('数据写入失败！');
		}
		$this->success('添加成功！', U('Academy/index'), 0);
	}

	public function deletePost() {
		if(!IS_POST) {
			$this->redirect('Academy/index');
		}
		$id = (int)I('post.id');
		$academy = D('Academy');
		if($academy->hasChildObj($id)) {
			$this->error($academy->getError());
		}
		$academy = M('Academy');
		if(M('Academy')->where(array('id' => $id))->delete()) {
			$this->success('删除成功！', U('Academy/index'), 0);
		} else {
			$this->error('删除失败！');
		}		
	}

	public function modifyPost() {
		if(!IS_POST) {
			$this->redirect('Academy/index');
		}
		$academy = D('Academy');
		if(!$academy->create()) {
			$this->error($academy->getError());
		}
		$result = $academy->save();
		if($result === false) {
			$this->error('数据更新失败！');
		}
		$this->success('修改成功！', U('Academy/index'), 0);
	}

	public function detail() {
		$id = (int)I('post.id');
		$this->ajaxReturn(M('Academy')->where(array('id' => $id))->find());
	}

	public function test() {
		dump(D('Major')->where(array('id' => 4))->relation('courses')->find());
	}

}
